﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;
namespace BusinessLogic
{
   public  static class DVT 
    {
        public static Entities.DVT[] Select(Entities.DVT nv)
        {
            Entities.DVT[] list = null;
            try
            {
                string sql = Common.Constants.DVT.SelectDVT;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.DVT.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.DVT row = new Entities.DVT();
                    row.DVTID = int.Parse(dr[Common.Constants.DVT.DVTID].ToString());
                    row.MaDonViTinh = dr[Common.Constants.DVT.MaDonViTinh].ToString();
                    row.TenDonViTinh = dr[Common.Constants.DVT.TenDonViTinh].ToString();
                    row.GhiChu = dr[Common.Constants.DVT.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.DVT.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.DVT[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.DVT)arr[i];
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            return list;
        }
        public static int InsertUpdate(Entities.DVT nhom)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.DVT.InsertUpdateDVT, cn);
                cmd.Parameters.Add(Common.Constants.DVT.HanhDong, SqlDbType.NVarChar).Value = nhom.HanhDong;
                cmd.Parameters.Add(Common.Constants.DVT.DVTID, SqlDbType.Int).Value = nhom.DVTID;
                cmd.Parameters.Add(Common.Constants.DVT.MaDonViTinh, SqlDbType.VarChar, 50).Value = nhom.MaDonViTinh;
                cmd.Parameters.Add(Common.Constants.DVT.TenDonViTinh, SqlDbType.NVarChar, 200).Value = nhom.TenDonViTinh;
                cmd.Parameters.Add(Common.Constants.DVT.GhiChu, SqlDbType.NVarChar, 200).Value = nhom.GhiChu;
                cmd.Parameters.Add(Common.Constants.DVT.Deleted, SqlDbType.Bit).Value = nhom.Deleted;
                rt = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
            return rt;
        }

        public static int Delete(Entities.DVT nhom)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.DVT.DeleteDVT, cn);
                cmd.Parameters.Add(Common.Constants.DVT.HanhDong, SqlDbType.VarChar).Value = nhom.HanhDong;
                cmd.Parameters.Add(Common.Constants.DVT.MaDonViTinh, SqlDbType.VarChar).Value = nhom.MaDonViTinh;
                rt = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }

        public static Entities.DVT[] Select()
        {
            Entities.DVT[] list = null;
            try
            {
                string sql = Common.Constants.DVT.SelectDVT;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.DVT row = new Entities.DVT();
                    row.DVTID = int.Parse(dr[Common.Constants.DVT.DVTID].ToString());
                    row.MaDonViTinh = dr[Common.Constants.DVT.MaDonViTinh].ToString();
                    row.TenDonViTinh = dr[Common.Constants.DVT.TenDonViTinh].ToString();
                    row.GhiChu = dr[Common.Constants.DVT.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.DVT.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.DVT[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.DVT)arr[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            return list;
        }
    }

}
